// import Vue from 'vue'
import store from '@/store'
import FormatNumber from '@/components/format-template/format-number'
import noFormat from '@/components/format-template/noFormat'
import FormatNumber1 from '@/components/format-template/format-number-0'
import FormatNumber2 from '@/components/format-template/format-number-0.0'
import FormatNumber3 from '@/components/format-template/format-number-0.00'
import FormatNumber4 from '@/components/format-template/format-number-0.000'
import FormatNumber5 from '@/components/format-template/format-number-0.0000'
import FormatNumber6 from '@/components/format-template/format-number-0.00000'
import FormatNumber7 from '@/components/format-template/format-number-1k.vue'
import FormatNumber8 from '@/components/format-template/format-number-0.00percent'

import FormatCurrency1 from '@/components/format-template/format-currency-dollar0'
import FormatCurrency2 from '@/components/format-template/format-currency-dollar0.0'
import FormatCurrency3 from '@/components/format-template/format-currency-dollar0.00'
import FormatCurrency4 from '@/components/format-template/format-currency-dollar0.000'
import FormatCurrency5 from '@/components/format-template/format-currency-dollar0.0000'
import FormatCurrency6 from '@/components/format-template/format-currency-0'
import FormatCurrency7 from '@/components/format-template/format-currency-0.0'
import FormatCurrency8 from '@/components/format-template/format-currency-0.00'
import FormatCurrency9 from '@/components/format-template/format-currency-0.000'
import FormatCurrency10 from '@/components/format-template/format-currency-0.0000'
import FormatCurrency11 from '@/components/format-template/format-currency-euro0'

import FormatDate from '@/components/format-template/format-date'
import FormatDate1 from '@/components/format-template/format-date-YYYY-MM-DD'
import FormatDate4 from '@/components/format-template/format-date-MM-DD-YY'
import FormatDate6 from '@/components/format-template/format-date-YYYY'
import FormatDate5 from '@/components/format-template/format-date-YY-MM-DD'
import FormatDate2 from '@/components/format-template/format-date-MM-YYYY'
import FormatDate3 from '@/components/format-template/format-datetime-MM-YYYY'
import FormatDatetime1 from '@/components/format-template/format-datetime-YYYY-MM-DD'
import FormatDatetime6 from '@/components/format-template/format-datetime-MM-DD-YY'
import FormatDatetime7 from '@/components/format-template/format-datetime-YY-MM-DD'
import FormatDatetime2 from '@/components/format-template/format-datetime-YYYY-MM-DD_HH.mm'
import FormatDatetime3 from '@/components/format-template/format-datetime-YYYY-MM-DD_HH.mm.ss'
import FormatDatetime4 from '@/components/format-template/format-datetime-YYYY-MM-DD_hh.mm.ss_am.pm'
import FormatDatetime5 from '@/components/format-template/format-datetime-YYYY-MM-DD_hh.mm.ss_AM_PM'
import FormatDatetime8 from '@/components/format-template/format-datetime-MM-DD-YYYY_hh.mm_am.pm'

import FormatBoolean1 from '@/components/format-template/format-boolean-1_0'
import FormatBoolean2 from '@/components/format-template/format-boolean-OK_NA'
import FormatBoolean3 from '@/components/format-template/format-boolean-On_Off'
import FormatBoolean4 from '@/components/format-template/format-boolean-True_False'
import FormatBoolean5 from '@/components/format-template/format-boolean-Active_InActive'
import FormatBoolean6 from '@/components/format-template/format-boolean-Yes_No'
import FormatBoolean7 from '@/components/format-template/format-boolean-Enable_Disable'

import FormatBHyperlink1 from '@/components/format-template/format-hyperlink-root'

import FormatText1 from '@/components/format-template/format-text_ticket_status'
import FormatText2 from '@/components/format-template/format-text_agv_status'
import FormatText3 from '@/components/format-template/format-text_ddj_status'
import {
  formatFunction
} from '@/utils/format'
import { generalHyperLinksFormat, returnSqlForFields, isHaveQuotationMarks } from '@/utils/common'
import { debounce } from 'lodash'

// const noFormat = Vue.component(
//   'bot-btn', {
//     template: `<div></div>`,
//     data () {
//       return {}
//     },
//     methods: {}
//   }
// )

export default {
  components: {
    noFormat,
    FormatNumber,
    FormatNumber1,
    FormatNumber2,
    FormatNumber3,
    FormatNumber4,
    FormatNumber5,
    FormatNumber6,
    FormatNumber7,
    FormatNumber8,
    FormatCurrency1,
    FormatCurrency2,
    FormatCurrency3,
    FormatCurrency4,
    FormatCurrency5,
    FormatCurrency6,
    FormatCurrency7,
    FormatCurrency8,
    FormatCurrency9,
    FormatCurrency10,
    FormatCurrency11,
    FormatDate1,
    FormatDate,
    FormatDate2,
    FormatDate3,
    FormatDate4,
    FormatDate5,
    FormatDate6,
    FormatDatetime1,
    FormatDatetime2,
    FormatDatetime3,
    FormatDatetime4,
    FormatDatetime5,
    FormatDatetime6,
    FormatDatetime7,
    FormatDatetime8,
    FormatBoolean1,
    FormatBoolean2,
    FormatBoolean3,
    FormatBoolean4,
    FormatBoolean5,
    FormatBoolean6,
    FormatBoolean7,
    FormatBHyperlink1,
    FormatText1,
    FormatText2,
    FormatText3
  },
  data () {
    return {
      formatForGrid: {
        noFormat: function () {
          return {
            template: noFormat
          }
        },
        formatNumber: function () {
          return {
            template: FormatNumber
          }
        },
        formatNumber1: function () {
          return {
            template: FormatNumber1
          }
        },
        formatNumber2: function () {
          return {
            template: FormatNumber2
          }
        },
        formatNumber3: function () {
          return {
            template: FormatNumber3
          }
        },
        formatNumber4: function () {
          return {
            template: FormatNumber4
          }
        },
        formatNumber5: function () {
          return {
            template: FormatNumber5
          }
        },
        formatNumber6: function () {
          return {
            template: FormatNumber6
          }
        },
        formatNumber7: function () {
          return {
            template: FormatNumber7
          }
        },
        formatNumber8: function () {
          return {
            template: FormatNumber8
          }
        },
        formatCurrency1: function () {
          return {
            template: FormatCurrency1
          }
        },
        formatCurrency2: function () {
          return {
            template: FormatCurrency2
          }
        },
        formatCurrency3: function () {
          return {
            template: FormatCurrency3
          }
        },
        formatCurrency4: function () {
          return {
            template: FormatCurrency4
          }
        },
        formatCurrency5: function () {
          return {
            template: FormatCurrency5
          }
        },
        formatCurrency6: function () {
          return {
            template: FormatCurrency6
          }
        },
        formatCurrency7: function () {
          return {
            template: FormatCurrency7
          }
        },
        formatCurrency8: function () {
          return {
            template: FormatCurrency8
          }
        },
        formatCurrency9: function () {
          return {
            template: FormatCurrency9
          }
        },
        formatCurrency10: function () {
          return {
            template: FormatCurrency10
          }
        },
        formatCurrency11: function () {
          return {
            template: FormatCurrency11
          }
        },
        formatDate1: function () {
          return {
            template: FormatDate1
          }
        },
        formatDate: function () {
          return {
            template: FormatDate
          }
        },
        formatDate2: function () {
          return {
            template: FormatDate2
          }
        },
        formatDate3: function () {
          return {
            template: FormatDate3
          }
        },
        formatDate4: function () {
          return {
            template: FormatDate4
          }
        },
        formatDate5: function () {
          return {
            template: FormatDate5
          }
        },
        formatDate6: function () {
          return {
            template: FormatDate6
          }
        },
        formatDatetime1: function () {
          return {
            template: FormatDatetime1
          }
        },
        formatDatetime2: function () {
          return {
            template: FormatDatetime2
          }
        },
        formatDatetime3: function () {
          return {
            template: FormatDatetime3
          }
        },
        formatDatetime4: function () {
          return {
            template: FormatDatetime4
          }
        },
        formatDatetime5: function () {
          return {
            template: FormatDatetime5
          }
        },
        formatDatetime6: function () {
          return {
            template: FormatDatetime6
          }
        },
        formatDatetime7: function () {
          return {
            template: FormatDatetime7
          }
        },
        formatDatetime8: function () {
          return {
            template: FormatDatetime8
          }
        },
        formatBoolean1: function () {
          return {
            template: FormatBoolean1
          }
        },
        formatBoolean2: function () {
          return {
            template: FormatBoolean2
          }
        },
        formatBoolean3: function () {
          return {
            template: FormatBoolean3
          }
        },
        formatBoolean4: function () {
          return {
            template: FormatBoolean4
          }
        },
        formatBoolean5: function () {
          return {
            template: FormatBoolean5
          }
        },
        formatBoolean6: function () {
          return {
            template: FormatBoolean6
          }
        },
        formatBoolean7: function () {
          return {
            template: FormatBoolean7
          }
        },
        formatBHyperlink1: function () {
          return {
            template: FormatBHyperlink1
          }
        },
        formatText1: function () {
          return {
            template: FormatText1
          }
        },
        formatText2: function () {
          return {
            template: FormatText2
          }
        },
        formatText3: function () {
          return {
            template: FormatText3
          }
        }
      },
      formatMap: {
        formatNumber: 'number',
        formatNumber1: 'number-0',
        formatNumber2: 'number-0.0',
        formatNumber3: 'number-0.00',
        formatNumber4: 'number-0.000',
        formatNumber5: 'number-0.0000',
        formatNumber6: 'number-0.00000',
        formatNumber7: 'number-1k',
        formatNumber8: 'number-0.00percent',
        formatCurrency1: 'currency-dollar0',
        formatCurrency2: 'currency-dollar0.0',
        formatCurrency3: 'currency-dollar0.00',
        formatCurrency4: 'currency-dollar0.000',
        formatCurrency5: 'currency-dollar0.0000',
        formatCurrency6: 'currency-0',
        formatCurrency7: 'currency-0.0',
        formatCurrency8: 'currency-0.00',
        formatCurrency9: 'currency-0.000',
        formatCurrency10: 'currency-0.0000',
        formatCurrency11: 'currency-euro0',
        formatDate1: 'date-YYYY-MM-DD',
        formatDate: 'date',
        formatDate4: 'date-MM-DD-YY',
        formatDate5: 'date-YY-MM-DD',
        formatDate2: 'date-MMM_YYYY',
        formatDate3: 'datetime-MMM_YYYY',
        formatDate6: 'date-YYYY',
        formatDatetime1: 'datetime-YYYY-MM-DD',
        formatDatetime6: 'datetime-MM-DD-YY',
        formatDatetime7: 'datetime-YY-MM-DD',
        formatDatetime2: 'datetime-YYYY-MM-DD_HH.mm',
        formatDatetime3: 'datetime-YYYY-MM-DD_HH.mm.ss',
        formatDatetime4: 'datetime-YYYY-MM-DD_hh.mm.ss_am.pm',
        formatDatetime5: 'datetime-YYYY-MM-DD_hh.mm.ss_AM.PM',
        formatDatetime8: 'datetime-MM-DD-YYYY_hh.mm_am.pm',
        formatBoolean1: 'boolean-1_0',
        formatBoolean2: 'boolean-OK_NA',
        formatBoolean3: 'boolean-On_Off',
        formatBoolean4: 'boolean-True_False',
        formatBoolean5: 'boolean-Active_InActive',
        formatBoolean6: 'boolean-Yes_No',
        formatBoolean7: 'boolean-Enable_Disable',
        formatBHyperlink1: 'FormatBHyperlink1',
        formatText1: 'text-ticket_status',
        formatText2: 'text-agv_status',
        formatText3: 'text-ddj_status'
      },
      formatComponentList: [],
      fxFields: []
    }
  },
  watch: {

  },
  computed: {

  },
  created () {

  },
  methods: {
    findTemplateName (formatName, businessType) {
      let dictList = this.getFormatList(businessType)
      let nameIndex = dictList.findIndex(e => e.dictValue === formatName)
      if (businessType === 'hyperlinks') {
        return 'formatBHyperlink1'
      }
      if (nameIndex > -1) {
        return dictList[nameIndex].subjectCode
      } else {
        return 'noFormat'
      }
    },
    findTableTemplateName (name) {
      if(name==='noFormat'){
        return name
      }else{
        return name?name.substr(0,1).toUpperCase()+name.substr(1):name
      }
    },
    getFormatList (businessType) {
      let temp = []
      switch (businessType) {
        case 'date':
          temp = window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_date').dataList
          break
        case 'datetime':
          temp = window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_datetime').dataList
          break
        case 'text':
          temp = window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_text').dataList
          break
        case 'boolean':
          temp = window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_boolean').dataList
          break
        case 'currency':
          temp = window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_currency').dataList
          break
        case 'number':
          temp = window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_number').dataList
          break
        case 'hyperlinks':
          temp.rootURL = JSON.parse(JSON.stringify(window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_hyperlinks_two').dataList))
          temp.rootURL.forEach((item) => {
            item.dictLabel = `{${item.dictLabel}}`
            item.subjectCode = store.state.user[item.subjectCode]
          })
          temp.rootURL = temp.rootURL.filter(item => {
            return !!item.subjectCode === true
          })
          temp.templateURL = JSON.parse(JSON.stringify(window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_fields_description_type_list_hyperlinks').dataList))
          temp.templateURL.forEach(item => {
            item.subjectCode = store.state.user[item.subjectCode]
          })
          temp.templateURL = temp.templateURL.filter(item => {
            return !!item.subjectCode === true
          })
          // temp.dataColumns = JSON.parse(JSON.stringify(this.datasetColumnsList))
          // temp.dataColumns.forEach(item => {
          //   item.alias = `{${item.alias}}`
          // })
          temp.dataColumns = []
          temp.subjectCode = 'formatBHyperlink1'
          break
        default:
          break
      }
      return temp
    },
    getFormatTemplateName (fieldsList, fieldName) {
      let fieldItemIndex = fieldsList.findIndex(e => e['bind'] === fieldName)
      // fieldItemIndex === -1 && (fieldItemIndex = fieldsList.findIndex(e => e['dataType'] === 'dimension'))
      // fieldItemIndex === -1 && (fieldItemIndex = fieldsList.findIndex(e => e['alias'] === 'dimension1'))
      if (fieldItemIndex === -1) {
        return 'noFormat'
      }
      try {
        let templateName = this.findTemplateName(fieldsList[fieldItemIndex].fieldsFormat, fieldsList[fieldItemIndex].businessType)
        if (fieldsList[fieldItemIndex].businessType === 'number' && fieldsList[fieldItemIndex].fieldsNewFormat) templateName = 'formatNumber'
        if (fieldsList[fieldItemIndex].businessType === 'date' || fieldsList[fieldItemIndex].businessType === 'datetime') templateName = 'formatDate'
        return templateName
      } catch (error) {

      }
    },
    setFormatFunctionForCardNumber (fieldsList, data, fieldName) {
      var fieldData = fieldsList.find(item => item.bind === fieldName)
      if (!fieldData) {
        var fieldItemIndex = fieldsList.findIndex(e => e['dataType'] === 'dimension')
        if (fieldItemIndex > -1) {
          fieldData = fieldsList[fieldItemIndex]
        }
      }
      if (Array.isArray(data) && data.length > 0) {
        let type = this.getFormatTemplateName(fieldsList, fieldName)
        return formatFunction(this.formatMap[type], data[0][fieldName], fieldData)
      } else {
        return 0
      }
    },
    setFormatFunctionForChart (fieldsList, value, fieldName) {
      var fieldData = fieldsList.find(item => item.bind === fieldName)
      if (!fieldData) {
        var fieldItemIndex = fieldsList.findIndex(e => e['dataType'] === 'dimension')
        if (fieldItemIndex > -1) {
          fieldData = fieldsList[fieldItemIndex]
        }
      }
      if (value === 'null') {
        return value
      }
      if (value === 'Grand Total') {
        return value
      }
      let type = this.getFormatTemplateName(fieldsList, fieldName)
      return formatFunction(this.formatMap[type], value, fieldData)
    },
    setFormatFunctionForDrillThrough (data, fieldsList) {
      data = this.setHyperlinkFormat(data, fieldsList, 'bind', 'gridgadget')
      for (let i = 0; i < data.length; i++) {
        for (let key in data[i]) {
          try {
            data[i][key] = this.setFormatFunctionForChart(fieldsList, data[i][key], key)
          } catch (error) {
          }
        }
      }
      return data
    },
    setDrillThroughInfo: debounce(function (params, gadgetType, originData, gadgetSetting) {
      if (params.name === 'null' || params.name === null) {
        params.name = null
      } else {
        params.name = params.name.toString()
      }
      if(params.name !== null && params.name.includes("'")){
        params.name = params.name.replace(/'/g, "''")
      }
      if (gadgetType === 'bar3dchartgadget') {
        let data = originData[params.dataIndex]
        let x = this.queryFields.find(i => i.bind === gadgetSetting.xaxis3d && i.dataType === 'dimension')
        let tempX = null
        let tempY = null
        if(x){
          if (x.dbType === 'datetime') {
            tempX =  data[gadgetSetting.xaxis3d] ?  this.dateTimeTypeDrillthrough(x, x.field, { name: data[gadgetSetting.xaxis3d] }) : `"${x.field}" is null`
          } else if (x.dbType === 'date') {
            tempX = data[gadgetSetting.xaxis3d] ? this.dateTypeDrillthrough(x, x.field, { name: data[gadgetSetting.xaxis3d] }) : `"${x.field}" is null`
          }
          else {
            tempX = data[gadgetSetting.xaxis3d] ? `"${x.field}" = '${data[gadgetSetting.xaxis3d]}'` : `"${x.field}" is null`
          }
        }
        let y = this.queryFields.find(i => i.bind === gadgetSetting.yaxis3d && i.dataType === 'dimension')
        if(y){
          if (y.dbType === 'datetime') {
            tempY = data[gadgetSetting.yaxis3d] ? this.dateTimeTypeDrillthrough(y, y.field, { name: data[gadgetSetting.yaxis3d] }) : `"${y.field}" is null`
          } else if (y.dbType === 'date') {
            tempY = data[gadgetSetting.yaxis3d] ? this.dateTypeDrillthrough(y, y.field, { name: data[gadgetSetting.yaxis3d] }) : `"${y.field}" is null`
          }        
          else {
            tempY = data[gadgetSetting.yaxis3d] ? `"${y.field}" = '${data[gadgetSetting.yaxis3d]}'` : `"${y.field}" is null`
          }
        }
        let sql1 =(x && y)? `${tempX} and ${tempY}` : ( x ? `${tempX}` : y ?`${tempY}`:'') 
        return this.findOriginData(sql1)
      }
      if(gadgetType === 'chartgridgadget'){
        sql='date = "2021-11-01"'
        return this.findOriginData(sql)
      }
      let getFieldsListItemIndex = field => {
        let i = this.queryFields.findIndex(e => e['alias'] === field)
        i === -1 && (i = this.queryFields.findIndex(e => e['dataType'] === 'dimension'))
        i === -1 && (i = this.queryFields.findIndex(e => e['alias'] === 'dimension1'))
        return i
      }
      let queryIdIndex = getFieldsListItemIndex(params.seriesName)
      if (queryIdIndex === -1) {
        this.queryId = null
      } else {
        this.queryId = this.queryFields[queryIdIndex].queryId
      }
      if (!this.queryId) return
      let getFieldsListIndex = queryId => {
        let i = this.queryFields.findIndex(e => e['queryId'] === queryId && e['dataType'] === 'dimension')
        return i
      }
      let sql = ''
      let fieldIndex = getFieldsListIndex(this.queryId) > -1 ? getFieldsListIndex(this.queryId) : getFieldsListItemIndex(this.gadgetSetting.valueFieldsNameMeasureX)
      if (fieldIndex === -1) {
        return false
      }
      var isNeedTrim = false
      let field = this.queryFields.findIndex(e => e['dataType'] === 'dimension' || e['dataType'] === 'measure') === -1 ? this.queryFields[fieldIndex].columnName : (this.queryFields[fieldIndex].fieldFx || this.queryFields[fieldIndex].field)
      if (gadgetType === 'piechartgadget') {
        if (this.gadgetSetting.valueFieldsNameMeasuresubX) {
          if (params.seriesIndex) {
            field = this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasuresubX).fieldFx ? this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasuresubX).fieldFx : this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasuresubX).field
          } else {
            field = this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX).fieldFx ? this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX).fieldFx : this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX).field
          }
        }
      }
      if (this.queryFields[fieldIndex].dbType === 'datetime') {
        sql = this.dateTimeTypeDrillthrough(this.queryFields[fieldIndex], field, params)
      } else {
        if (this.queryFields[fieldIndex].dbType === 'date') {
          sql = this.dateTypeDrillthrough(this.queryFields[fieldIndex], field, params)
        } else {
          if (params.name === null) {
            params.name = ' is null'
          } else if (params.name === ' ') {
            isNeedTrim = true
            params.name = "= ''"
          } else {
            params.name = "= '" + params.name + "'"
          }
          sql = ''
          let ifHaveQuotationMarks = isHaveQuotationMarks(field)
          if (isNeedTrim) {
            sql = ifHaveQuotationMarks ? `trim(${field})` + `${params.name}` : `trim("${field}")` + `${params.name}`
          } else {
            sql = ifHaveQuotationMarks ? `${field}` + `${params.name}` : `"${field}"` + `${params.name}`
          }
        }
      }
      if (this.queryFields.findIndex(e => e['dataType'] === 'measure') === -1 || this.queryFields.findIndex(e => e['dataType'] === 'dimension') === -1) {
        sql = ''
      }
      if (gadgetType === 'piechartgadget') {
        if (this.gadgetSetting.valueFieldsNameMeasuresubX) {
          if (params.seriesIndex) {
            let fatherData = originData[params.dataIndex]
            params.name = fatherData[this.gadgetSetting.valueFieldsNameMeasureX]
            if(params.name === 'null' || params.name === null){
              params.name = null
            } else {
              params.name = params.name.toString()
            }
            if(params.name !== null && params.name.includes("'")){
              params.name = params.name.replace(/'/g, "''")
            }
            let fatherField = this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX).fieldFx ? this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX).fieldFx : this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX).field
            let fatherDbType = this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX).dbType
            let sql1 = ''
            if (fatherDbType === 'datetime') {
              sql1 = this.dateTimeTypeDrillthrough(this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX), fatherField, params)
            } else {
              if (fatherDbType === 'date') {
                sql1 = this.dateTypeDrillthrough(this.queryFields.find(item => item.alias === this.gadgetSetting.valueFieldsNameMeasureX), fatherField, params)
              } else {
                if (params.name === null) {
                  params.name = ' is null'
                } else if (params.name === ' ') {
                  isNeedTrim = true
                  params.name = "= ''"
                } else {
                  params.name = "= '" + params.name + "'"
                }
                sql1 = ''

                let ifHaveQuotationMarks2 = isHaveQuotationMarks(fatherField)
                if (isNeedTrim) {
                  sql1 = ifHaveQuotationMarks2 ? `trim(${fatherField})` + `${params.name}` : `trim("${fatherField}")` + `${params.name}`
                } else {
                  sql1 = ifHaveQuotationMarks2 ? `${fatherField}` + `${params.name}` : `"${fatherField}"` + `${params.name}`
                }
              }
            }
            sql += ` and ${sql1}`
          }
        }
      }
      field && this.findOriginData(sql)
    }, 200),
    dateTypeDrillthrough (fieldItem, field, params) {
      let ifHaveQuotationMarks = isHaveQuotationMarks(field)
      let tempSql = ''
      let timeZoneToUTC = `at time zone '${this.$store.state.user.timezoneId}'`
      if (fieldItem.groupType === 'date') {
        if (this.$store.state.user.timezoneOffset && fieldItem.dbType === 'datetime') {
          if (fieldItem.businessType && fieldItem.businessType === 'date') {
            if (fieldItem.period === 'weeks(sun-sat)') {
              if (params.name === null) {
                tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '1 days') + interval '-1 days' is null` : `date_trunc('weeks',"${field}" + interval '1 days') + interval '-1 days' is null`
              } else {
                tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '1 days') + interval '-1 days' = '${params.name}'` : `date_trunc('weeks',"${field}" + interval '1 days') + interval '-1 days' = '${params.name}'`
              }
            } else {
              if (fieldItem.period === 'weeks(sat-fri)') {
                if (params.name === null) {
                  tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '2 days') + interval '-2 days' is null` : `date_trunc('weeks',"${field}" + interval '2 days') + interval '-2 days' is null`
                } else {
                  tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '2 days') + interval '-2 days' = '${params.name}'` : `date_trunc('weeks',"${field}" + interval '2 days') + interval '-2 days' = '${params.name}'`
                }
              } else {
                if (params.name === null) {
                  tempSql = ifHaveQuotationMarks ? `date_trunc('${fieldItem.period}',${field}) is null` : `date_trunc('${fieldItem.period}',"${field}") is null`
                } else {
                  tempSql = ifHaveQuotationMarks ? `date_trunc('${fieldItem.period}',${field}) = '${params.name}'` : `date_trunc('${fieldItem.period}',"${field}") = '${params.name}'`
                }
              }
            }
          } else {
            if (fieldItem.period === 'weeks(sun-sat)') {
              // tempSql = `date_trunc('weeks',"${field}" + interval '${this.$store.state.user.timezoneOffset}') - interval '${this.$store.state.user.timezoneOffset}' + interval '-1 days' = '${params.name}'`
              if (params.name === null) {
                tempSql = `date_trunc('weeks',${returnSqlForFields(field)}) ${timeZoneToUTC}  + interval '-1 days' is null`
              } else {
                tempSql = `date_trunc('weeks',${returnSqlForFields(field)}) ${timeZoneToUTC}  + interval '-1 days' = '${params.name}'`
              }
            } else {
              if (fieldItem.period === 'weeks(sat-fri)') {
                // tempSql = `date_trunc('weeks',"${field}" + interval '${this.$store.state.user.timezoneOffset}') - interval '${this.$store.state.user.timezoneOffset}' + interval '-2 days' = '${params.name}'`
                if (params.name === null) {
                  tempSql = `date_trunc('weeks',${returnSqlForFields(field)}) ${timeZoneToUTC}  + interval '-2 days' is null`
                } else {
                  tempSql = `date_trunc('weeks',${returnSqlForFields(field)}) ${timeZoneToUTC}  + interval '-2 days' = '${params.name}'`
                }
              } else {
                // tempSql = `date_trunc('${fieldItem.period}',"${field}" + interval '${this.$store.state.user.timezoneOffset}') - interval '${this.$store.state.user.timezoneOffset}' = '${params.name}'`
                if (params.name === null) {
                  tempSql = `date_trunc('${fieldItem.period}',${returnSqlForFields(field)}) ${timeZoneToUTC}  is null`
                } else {
                  tempSql = `date_trunc('${fieldItem.period}',${returnSqlForFields(field)}) ${timeZoneToUTC}  = '${params.name}'`
                }
              }
            }
          }
        } else {
          if (fieldItem.period === 'weeks(sun-sat)') {
            if (params.name === null) {
              tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '1 days') + interval '-1 days' is null` : `date_trunc('weeks',"${field}" + interval '1 days') + interval '-1 days' is null`
            } else {
              tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '1 days') + interval '-1 days' = '${params.name}'` : `date_trunc('weeks',"${field}" + interval '1 days') + interval '-1 days' = '${params.name}'`
            }
          } else {
            if (fieldItem.period === 'weeks(sat-fri)') {
              if (params.name === null) {
                tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '2 days') + interval '-2 days' is null` : `date_trunc('weeks',"${field}" + interval '2 days') + interval '-2 days' is null`
              } else {
                tempSql = ifHaveQuotationMarks ? `date_trunc('weeks',${field} + interval '2 days') + interval '-2 days' = '${params.name}'` : `date_trunc('weeks',"${field}" + interval '2 days') + interval '-2 days' = '${params.name}'`
              }
            } else {
              if (params.name === null) {
                tempSql = ifHaveQuotationMarks ? `date_trunc('${fieldItem.period}',${field}) is null` : `date_trunc('${fieldItem.period}',"${field}") is null`
              } else {
                tempSql = ifHaveQuotationMarks ? `date_trunc('${fieldItem.period}',${field}) = '${params.name}'` : `date_trunc('${fieldItem.period}',"${field}") = '${params.name}'`
              }
            }
          }
        }
      }
      if (fieldItem.groupType === 'number') {
        if (fieldItem.period === 'weekday') {
          // tempSql = `date_part('isodow',"${field}" + interval '${this.$store.state.user.timezoneOffset}') = '${params.name}'`
          if (params.name === null) {
            tempSql = `date_part('isodow',${returnSqlForFields(field)}) is null`
          } else {
            tempSql = `date_part('isodow',${returnSqlForFields(field)}) = '${params.name}'`
          }
        } else {
          // tempSql = `date_part('${fieldItem.period}',"${field}" + interval '${this.$store.state.user.timezoneOffset}') = '${params.name}'`
          if (params.name === null) {
            tempSql = `date_part('${fieldItem.period}',${returnSqlForFields(field)}) is null`
          } else {
            tempSql = `date_part('${fieldItem.period}',${returnSqlForFields(field)}) = '${params.name}'`
          }
        }
      }

      return tempSql
    },
    dateTimeTypeDrillthrough (fieldItem, field, params) {
      let ifHaveQuotationMarks = isHaveQuotationMarks(field)
      let tempSql = ''
      let timeZoneToUTC = `at time zone '${this.$store.state.user.timezoneId}'`
      if (fieldItem.groupType === 'date') {
        if (fieldItem.period === 'weeks(sun-sat)') {
          if (params.name === null) {
            tempSql = ifHaveQuotationMarks ? `group_week_sun_to_sat(${field}, '${this.$store.state.user.timezoneId}') is null` : `group_week_sun_to_sat("${field}", '${this.$store.state.user.timezoneId}') is null`
          } else {
            tempSql = ifHaveQuotationMarks ? `group_week_sun_to_sat(${field}, '${this.$store.state.user.timezoneId}') = '${params.name}'` : `group_week_sun_to_sat("${field}", '${this.$store.state.user.timezoneId}') = '${params.name}'`
          }
        } else {
          if (fieldItem.period === 'weeks(sat-fri)') {
            if (params.name === null) {
              tempSql = ifHaveQuotationMarks ? `group_week_sat_to_fri(${field}, '${this.$store.state.user.timezoneId}') is null` : `group_week_sat_to_fri("${field}", '${this.$store.state.user.timezoneId}') is null`
            } else {
              tempSql = ifHaveQuotationMarks ? `group_week_sat_to_fri(${field}, '${this.$store.state.user.timezoneId}') = '${params.name}'` : `group_week_sat_to_fri("${field}", '${this.$store.state.user.timezoneId}') = '${params.name}'`
            }
          } else {
            if (params.name === null) {
              tempSql = `date_trunc('${fieldItem.period}',${returnSqlForFields(field)}) ${timeZoneToUTC} is null`
            } else {
              tempSql = `date_trunc('${fieldItem.period}',${returnSqlForFields(field)}) ${timeZoneToUTC} = '${params.name}'`
            }
          }
        }
      }
      if (fieldItem.groupType === 'number') {
        if (fieldItem.period === 'weekday') {
          if (params.name === null) {
            tempSql = `date_part('isodow',${returnSqlForFields(field)}) is null`
          } else {
            tempSql = `date_part('isodow',${returnSqlForFields(field)}) = '${params.name}'`
          }
        } else {
          if (params.name === null) {
            tempSql = `date_part('${fieldItem.period}',${returnSqlForFields(field)}) is null`
          } else {
            tempSql = `date_part('${fieldItem.period}',${returnSqlForFields(field)}) = '${params.name}'`
          }
        }
      }
      return tempSql
    },
    setHyperlinkFormat (data, fieldsList, field, gadgetType) {
      if (Array.isArray(data) && Array.isArray(fieldsList)) {
        let newData = JSON.parse(JSON.stringify(data))
        data.map((item, index) => {
          for (var key in item) {
            let resultIndex = fieldsList.findIndex(col => col[field] === key)
            if (resultIndex < 0) {
              resultIndex = fieldsList.findIndex(col => col.dataType === 'dimension')
            }
            if (resultIndex > -1 && fieldsList[resultIndex].businessType === 'hyperlinks') {
              item[key] = generalHyperLinksFormat(item[key], fieldsList[resultIndex].fieldsFormat ? fieldsList[resultIndex].fieldsFormat : '', gadgetType, fieldsList[resultIndex].isNewPage, newData[index], fieldsList)
            }
          }
        })
      }
      return data
    },
    setDrillThroughInfoMoreDimension: debounce(function (params) {
      let queryIdArr = Array.from(new Set(this.queryFields.map(item=>item.queryId)))     
      let tempList = []
      let selectList = []
      let queryFieldsArr
      if(queryIdArr.length>1) {
        queryFieldsArr = this.queryFields.filter(item=>item.queryId===queryIdArr[0]) ||[]
      } else {
        queryFieldsArr = this.queryFields
      }
      // let queryFieldsArr = this.queryFields.filter(item=>item.queryId===queryId) ||[]
      for (let index = 0; index < queryFieldsArr.length; index++) {
        let element = queryFieldsArr[index]
        if (element.dataType === 'dimension') {
          selectList.push(element)
        }
      }
      // this.gadgetSetting.valueFieldsNameMeasureGroup && this.gadgetSetting.valueFieldsNameMeasureGroup !== 'group' &&
      if (selectList.length > 1 && params.selectObj) {
        for (let i = 0; i < selectList.length; i++) {
          const fieldObj = selectList[i]
          let fieldItem = ''
          if (fieldObj.dbType && fieldObj.dbType === 'boolean') {
            fieldItem =  params.selectObj[fieldObj.bind] || params.selectObj[`dimension${i+1}`] 
          } else {
            if (fieldObj.dbType && fieldObj.dbType === 'number') {
              if (params.selectObj[fieldObj.bind] === null) {
                fieldItem = null
              } else {
                fieldItem = params.selectObj[fieldObj.bind]|| params.selectObj[`dimension${i+1}`] 
              }              
            } else {
              if (params.selectObj[fieldObj.bind] === null) {
                fieldItem = null
              }else{              
                fieldItem = params.selectObj[fieldObj.bind] || params.selectObj[`dimension${i+1}`]
              }
              }
          }
          tempList.push({
            item: this.executeDrillSQL(fieldObj, fieldItem)
          })
        }
      }
      let sql = ''
      if (tempList && tempList.length > 0) {
        for (let i = 0; i < tempList.length; i++) {
          if (i === tempList.length - 1) {
            sql += tempList[i].item
          } else {
            sql += tempList[i].item + ' and '
          }
        }
      }
      this.findOriginData(sql)
    }, 200),
    booleanTypeDrillthrough (fieldItem, field, params, fieldFx) {
      let ifHaveQuotationMarks = isHaveQuotationMarks(field)
      if (fieldFx) {
        params.name = "'" + params.name + "'"
      }
      let tempSql = ''
      if ((params.name === null)) {
        tempSql = ifHaveQuotationMarks ? field + ' is null' : '"' + field + '"' + ' is null'
      } else {
        tempSql = ifHaveQuotationMarks ? field + ' = ' + `${params.name}` : '"' + field + '" = ' + `${params.name}`
      }
      return tempSql
    },
    executeDrillSQL (fieldObj, fieldItem) {
      let sqlItem = []
      if (fieldObj.dbType === 'datetime') {
        sqlItem = this.dateTimeTypeDrillthrough(fieldObj, fieldObj.fieldFx || fieldObj.field || '', { name: fieldItem })
        return sqlItem
      }
      if (fieldObj.dbType === 'date') {
        sqlItem = this.dateTypeDrillthrough(fieldObj, fieldObj.fieldFx || fieldObj.field || '', { name: fieldItem })
        return sqlItem
      }
      if (fieldObj.dbType === 'boolean') {
        sqlItem = this.booleanTypeDrillthrough(fieldObj, fieldObj.fieldFx || fieldObj.field || '', { name: fieldItem }, !!fieldObj.fieldFx)
        return sqlItem
      } else {
        if (fieldObj.fieldFx) {
          sqlItem = fieldObj.fieldFx + "='" + fieldItem + "'"
        } else {
          var isNeedTrim = false
          // Todo
          let ifHaveQuotationMarks = isHaveQuotationMarks(fieldObj.field)
          if (fieldItem === null) {
            fieldItem = ' is null'
          } else if (fieldItem === ' ') {
            isNeedTrim = true
            fieldItem = "= ''"
          } else {
            fieldItem = "= '" + fieldItem + "'"
          }
          if (isNeedTrim) {
            sqlItem = ifHaveQuotationMarks ? `trim(${fieldObj.field})` + `${fieldItem}` : `trim("${fieldObj.field}")` + `${fieldItem}`
          } else {
            sqlItem = ifHaveQuotationMarks ? (fieldObj.field ? fieldObj.field : '') + fieldItem : '"' + (fieldObj.field ? fieldObj.field : '') + '"' + fieldItem
          }
        }
      }

      return sqlItem
    }
  }
}
